package com.wuwei.elearning.test.e_learning;

import com.alibaba.excel.EasyExcel;
import com.wuwei.elearning.question.dao.DataDAO;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.domain.QuestionTranslated;
import com.wuwei.elearning.question.listener.UploadDataListener;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.question.mapper.QuestionTranslatedMapper;
import com.wuwei.elearning.question.mapper.SubjectMapper;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@SpringBootTest
@ActiveProfiles("test")
public class UpdateQuestion {

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private QuestionTranslatedMapper questionTranslatedMapper;

    @Resource
    private QuestionMapper questionMapper;

    @SneakyThrows
    public Res testQuestion(String  path) {
//        public static void main(String[] args) throws Exception {
//        String path = "C:\\Users\\SIM\\Desktop\\123.txt";
//        String path = "C:\\Users\\SIM\\Desktop\\111.txt";
        BufferedReader reader = Files.newBufferedReader(Paths.get(path));
        String line;
        Question question;
        List<Question> questions = new ArrayList<>();
        QuestionTranslated questionTranslated;
        List<QuestionTranslated> questionTranslateds = new ArrayList<>();
        while ((line = reader.readLine()) != null) {
            if (line.contains("id:")) {
                question=new Question();
                questionTranslated = new QuestionTranslated();

                //提取问题
                question.setQuestion(line.substring(line.indexOf(":")+1, line.indexOf("          id:")));

                //提取id
                String id = line.substring(line.indexOf("id:") + 3);
                question.setId(Long.valueOf(id));
                questionTranslated.setQuestionId(Long.valueOf(id));

                //提取英文题目
                line =  reader.readLine();
                questionTranslated.setQuestion(line.substring(line.indexOf(":")+1));

                //提取选项1
                line =  reader.readLine();
                question.setItem1(line.substring(2).trim());

                //提取英文选项1
                line = reader.readLine();
                questionTranslated.setItem1(line.substring(2).trim());

                //提取选项2
                line =  reader.readLine();
                question.setItem2(line.substring(2).trim());

                //提取英文选项2
                line = reader.readLine();
                questionTranslated.setItem2(line.substring(2).trim());

                //提取选项3
                line =  reader.readLine();
                question.setItem3(line.substring(2).trim());

                //提取英文选项3
                line = reader.readLine();
                questionTranslated.setItem3(line.substring(2).trim());

                //提取选项4
                line =  reader.readLine();
                question.setItem4(line.substring(2).trim());

                //提取英文选项4
                line = reader.readLine();
                questionTranslated.setItem4(line.substring(2).trim());

                //提取答案
                line =  reader.readLine();
                question.setAnswer(line.substring(line.indexOf("答案：")+3));
                questionTranslated.setAnswer(line.substring(line.indexOf("答案：")+3));
                questions.add(question);
                questionTranslateds.add(questionTranslated);
            }
        }
        checkQuestions(questions);
        Res res = new Res();
        res.setQuestions(questions);
        res.setQuestionTranslateds(questionTranslateds);
        return res;
    }

    public void checkQuestions(List<Question> questions){
        for (Question question : questions) {
            Long questionId = question.getId();
            Question selected = questionMapper.selectById(questionId);
            boolean compared = compareQuestion(question, selected);
            if (!compared) {
                System.out.println("error");
            }
        }
    }

    /**
     * 比较question和selected的question，item1，item2，item3，item4，answer是否完全一样
     * @param question
     * @param selected
     * @return 如果完全一样，返回true，如果不一样，返回false
     */
    private boolean compareQuestion(Question question, Question selected) {
        return question.getQuestion().equals(selected.getQuestion()) &&
                question.getItem1().equals(selected.getItem1()) &&
                question.getItem2().equals(selected.getItem2()) &&
                question.getItem3().equals(selected.getItem3()) &&
                question.getItem4().equals(selected.getItem4()) &&
                question.getAnswer().equals(selected.getAnswer());
    }

    @Resource
    private DataDAO<QuestionTranslated> dao;

    public Res testQuestionTranslated() {
        List<Question> questions = new ArrayList<>();
        List<QuestionTranslated> questionTranslateds = new ArrayList<>();
        EasyExcel.read("C:\\Users\\SIM\\Desktop\\新增加的12题.xlsx", QuestionTranslated.class, new UploadDataListener<QuestionTranslated>(dao)).sheet().doRead();
        List<QuestionTranslated> list = dao.getList();
        for (QuestionTranslated questionTranslated1 : list) {
            QuestionTranslated questionTranslated = new QuestionTranslated();
            Question question = new Question();
            String q = questionTranslated1.getQuestion();
            String[] split = q.split("\n");
            question.setQuestion(split[0]);
            questionTranslated.setQuestion(split[1]);
            String item1 = questionTranslated1.getItem1();
            split = item1.split("\n");
            question.setItem1(split[0]);
            questionTranslated.setItem1(split[1]);
            String item2 = questionTranslated1.getItem2();
            split = item2.split("\n");
            question.setItem2(split[0]);
            questionTranslated.setItem2(split[1]);
            String item3 = questionTranslated1.getItem3();
            split = item3.split("\n");
            question.setItem3(split[0]);
            questionTranslated.setItem3(split[1]);
            String item4 = questionTranslated1.getItem4();
            split = item4.split("\n");
            question.setItem4(split[0]);
            questionTranslated.setItem4(split[1]);
            String answer = questionTranslated1.getAnswer();
            split = answer.split("\n");
            question.setAnswer(split[0]);
            questionTranslated.setAnswer(split[1]);
            Long questionId = questionTranslated1.getQuestionId();
            question.setId(questionId);
            questionTranslated.setQuestionId(questionId);
            questions.add(question);
            questionTranslateds.add(questionTranslated);
        }
        Res res = new Res();
        res.setQuestions( questions);
        res.setQuestionTranslateds(questionTranslateds);
        return res;
    }

    @Test
    public void getQue(){
        List<Question> questions = new ArrayList<>();
        List<QuestionTranslated> questionTranslateds = new ArrayList<>();
        String path1 = "C:\\Users\\SIM\\Desktop\\111.txt";
        String path2 = "C:\\Users\\SIM\\Desktop\\123.txt";
        Res res1 = testQuestion(path1);
        List<Question> questions1 = res1.getQuestions();
        List<QuestionTranslated> questionTranslateds1 = res1.getQuestionTranslateds();
        checkQuesTranslate(questionTranslateds1);
        Res res2 = testQuestion(path2);
        List<Question> questions2 = res2.getQuestions();
        List<QuestionTranslated> questionTranslateds2 = res2.getQuestionTranslateds();
        checkQuesTranslate(questionTranslateds2);
        Res res3 = testQuestionTranslated();
        List<Question> questions3 = res3.getQuestions();
        List<QuestionTranslated> questionTranslateds3 = res3.getQuestionTranslateds();
        checkQuesTranslate(questionTranslateds3);
        checkQuestions( questions3);
        questions.addAll(questions1);
        questions.addAll(questions2);
        questions.addAll(questions3);

        questionTranslateds.addAll(questionTranslateds1);
        questionTranslateds.addAll(questionTranslateds2);
        questionTranslateds.addAll(questionTranslateds3);

        System.out.println(questions);
        System.out.println(questionTranslateds);
        finalCheck(questions,questionTranslateds);
        for (QuestionTranslated questionTranslated : questionTranslateds) {
            questionTranslatedMapper.insert(questionTranslated);
        }
    }

    private void finalCheck(List<Question> questions, List<QuestionTranslated> questionTranslateds) {
        EasyExcel.write("DA42中文",Question.class).sheet().doWrite(questions);
        EasyExcel.write("DA42英文文",QuestionTranslated.class).sheet().doWrite(questionTranslateds);
        for (QuestionTranslated questionTranslated : questionTranslateds) {
            Long questionId = questionTranslated.getQuestionId();
            Question question = questionMapper.selectById(questionId);
            if (question==null) {
                System.out.println("error");
            }
        }
    }

    private void checkQuesTranslate(List<QuestionTranslated> questionTranslateds) {
        for (QuestionTranslated questionTranslated : questionTranslateds) {
            if (questionTranslated.getQuestion()==null||questionTranslated.getItem1()==null||
                    questionTranslated.getItem2()==null||questionTranslated.getItem3()==null||
                    questionTranslated.getItem4()==null||questionTranslated.getAnswer()==null||
                    questionTranslated.getQuestion().isEmpty() || questionTranslated.getItem1().isEmpty() ||
                    questionTranslated.getItem2().isEmpty() || questionTranslated.getItem3().isEmpty() ||
                    questionTranslated.getItem4().isEmpty() || questionTranslated.getAnswer().isEmpty()
            ) {
                System.out.println("error");
            }

        }
    }
}
